"""
Case Type   : 功能测试
Case Name   : revoke收回用户全权限，on user后加多个用户
Create At   : 2024-11-29
Owner       : wan005
Description :
        1.创建用户
        2.授予用户权限
        3.回收用户权限，on user后加多个用户
Expect      :
        1.用户创建成功
        2.授权成功
        3.报错
History     :
"""
import os
import unittest
from yat.test import macro
from testcase.utils.Common import Common
from testcase.utils.Constant import Constant
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger


class Compatibility(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.pri_sh = CommonSH('PrimaryDbBmode')
        self.common = Common()
        self.constant = Constant()
        self.user1 = 'user_revoke_001'
        self.user2 = 'user_revoke_002'
        self.user3 = 'user_revoke_003'

    def test_revoke(self):
        text = '-----step1:创建多个普通用户;expect:创建成功-----'
        self.log.info(text)
        sql1 = f'create user {self.user1} password \'{macro.COMMON_PASSWD}\';' \
               f'create user {self.user2} password \'{macro.COMMON_PASSWD}\';' \
               f'create user {self.user3} password \'{macro.COMMON_PASSWD}\';'
        res1 = self.pri_sh.execut_db_sql(sql1)
        self.log.info(res1)
        self.assertEqual(3, res1.count(
            self.constant.CREATE_ROLE_SUCCESS_MSG), f'执行失败: {text}')

        text = '----step2:授予用户权限;expect:成功----'
        self.log.info(text)
        sql2 = f'grant proxy on {self.user1} to {self.user3};' \
               f'grant proxy on {self.user2} to {self.user3} with grant option;'
        res2 = self.pri_sh.execut_db_sql(sql2)
        self.log.info(res2)
        self.assertEqual(2, res2.count('GRANT ROLE'), f'执行失败: {text}')

        text = '----step3:回收用户权限，on user后加多个用户;expect:失败----'
        self.log.info(text)
        sql3 = f'revoke proxy on {self.user1},{self.user2} from {self.user3};'
        res3 = self.pri_sh.execut_db_sql(sql3)
        self.log.info(res3)
        self.assertIn(f'syntax error at or near ","', res3, f'执行失败: {text}')

    def tearDown(self):
        text = '----清理环境----'
        self.log.info(text)
        sql_cle1 = f'drop user {self.user1},{self.user2},{self.user3} cascade;'
        res_cle1 = self.pri_sh.execut_db_sql(sql_cle1)
        self.log.info(res_cle1)
        self.assertEqual(self.constant.DROP_ROLE_SUCCESS_MSG, res_cle1, 
                         f'执行失败: {text}')
        self.log.info(f'-----{os.path.basename(__file__)} end-----')